package wang.relish.widget.vehicleedittext;

import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.widget.EditText;

/**
 * 作者：huyi
 * 日期：2020/3/27 9:43
 * 描述：
 */
public class OnkeyBoardActionListener {
    private VehicleKeyboardView keyboardView;
    private EditText mEditText;
    private OnkeyCallBack onkeyCallBack;

    public OnkeyBoardActionListener(VehicleKeyboardView keyboardView, OnkeyCallBack onkeyCallBack) {
        this.keyboardView = keyboardView;
        this.onkeyCallBack = onkeyCallBack;
        init();
    }

    public EditText getmEditText() {
        return mEditText;
    }

    public void setmEditText(EditText mEditText) {
        this.mEditText = mEditText;
    }

    private void init() {
        if (keyboardView == null) {
            return;
        }

        keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
            @Override
            public void onPress(int primaryCode) {

            }

            @Override
            public void onRelease(int primaryCode) {

            }

            @Override
            public void onKey(int primaryCode, int[] keyCodes) {
                if (mEditText == null) return;

                if (onKeyEvent(primaryCode, keyCodes)) {
                    return;
                }
                if (primaryCode == Keyboard.KEYCODE_DELETE) { // 回退(backspace)
                    int start = Math.max(mEditText.getSelectionStart(), 0);
                    int end = Math.max(mEditText.getSelectionEnd(), 0);
                    if (start != end) {
                        mEditText.getText().delete(start, end);
                    } else if (start > 0) {
                        mEditText.getText().delete(start - 1, end);
                    }
                } else if (primaryCode == Keyboard.KEYCODE_CANCEL) { // 完成（complete）
                    close();
                } else if (primaryCode == Keyboard.EDGE_LEFT) {
                    int start = mEditText.getSelectionStart();
                    if (start > 0) mEditText.setSelection(start - 1);
                } else if (primaryCode == Keyboard.EDGE_RIGHT) {
                    int start = mEditText.getSelectionStart();
                    if (start < mEditText.length()) mEditText.setSelection(start + 1);
                } else {
                    String text = Character.toString((char) primaryCode);
                    int start = Math.max(mEditText.getSelectionStart(), 0);
                    int end = Math.max(mEditText.getSelectionEnd(), 0);
                    mEditText.getText().replace(Math.min(start, end), Math.max(start, end),
                            text, 0, text.length());
                }
            }

            @Override
            public void onText(CharSequence text) {

            }

            @Override
            public void swipeLeft() {

            }

            @Override
            public void swipeRight() {

            }

            @Override
            public void swipeDown() {

            }

            @Override
            public void swipeUp() {

            }
        });
    }

    /**
     * （用于拓展）处理特殊的键盘按键响应
     *
     * @param primaryCode 主键值
     * @param keyCodes    所有键值
     */
    public boolean onKeyEvent(int primaryCode, int[] keyCodes) {
        if (-100 == primaryCode) {//语音
            if (onkeyCallBack != null) {
                onkeyCallBack.voice();
            }
            return true;
        }
        return false;
    }

    /**
     * 关闭键盘
     */
    public void close() {
        if (onkeyCallBack != null) {
            onkeyCallBack.close();
        }
    }
}
